package com.dearbinge.opentapi.content.moments;

import com.dearbinge.data.api.moments.BusinessCircleCommentService;
import com.dearbinge.data.pojo.moments.vo.BusinessCircleCommentVO;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 社交圈评论控制层
 *
 * @author wes
 * @date 18:22 2019/1/4
 */
@Api(tags = "1.2、社交圈-社交圈评论")
@RestController
@RequestMapping("/momentsComment")
@EnableAutoConfiguration
public class BusinessCircleCommentController extends WebMvcConfigurerAdapter {

    @Autowired
    private BusinessCircleCommentService businessCircleCommentService;


    /**
     * 社交圈评论增加 修改
     */
    @ApiOperation(value = "社交圈评论：新增（id为空||id为0）/修改", notes = "社交圈评论：新增（id为空||id为0）/修改")
    @RequestMapping(value = "/addOrUpdateBusinessCircleComment", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdateBusinessCircleComment(@RequestBody BusinessCircleCommentVO comment) {
        try {
            Integer dataCode;
            if (comment.getCommentId() != null && comment.getCommentId() != 0) {
                dataCode = businessCircleCommentService.update(comment);
                if (dataCode > 0) {
                    return ResultMsg.ok("修改成功");
                }
            } else {
                dataCode = businessCircleCommentService.insert(comment);
                if (dataCode > 0) {
                    return ResultMsg.ok("添加成功");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    @ApiOperation(value = "删除评论", notes = "删除评论")
    @ApiImplicitParam(name = "id", value = "主键id", paramType = "query", required = true)
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = businessCircleCommentService.delete(id);
            if (dataCode > 0) {
                return ResultMsg.ok("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    @ApiOperation(value = "根据id查询评论回复", notes = "根据id查询评论回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "生活/商品表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "tableType", value = "1：生活表，2：商品表", paramType = "query", required = true)
    })
    @RequestMapping(value = "/listCommentById", method = RequestMethod.GET)
    public ResultMsg<List<BusinessCircleCommentVO>> listCommentById(Integer tableId, Integer tableType) {
        try {
            List<BusinessCircleCommentVO> list = businessCircleCommentService.listCommentById(tableType, tableId);
            return ResultMsg.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }
}

